package com.fss.article.service.impl;

import com.fss.article.domain.Article;
import com.fss.article.domain.ArticlePo;
import com.fss.article.domain.MyComment;
import com.fss.article.mapper.ArticleMapper1;
import com.fss.article.service.ArticleService1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ArticleService1Impl1 implements ArticleService1 {

    @Autowired
    private ArticleMapper1 articleMapper1;
    @Override
    public List<ArticlePo> getCategorys() {

        List<String> categorys = articleMapper1.getCategorys();
        List<ArticlePo> articlePos=new ArrayList<>();
        for(int i=0;i<categorys.size();i++)
        {
            ArticlePo tmp=new ArticlePo();
            tmp.setCategory(categorys.get(i));
            tmp.setArticles(new ArrayList<Article>());
            articlePos.add(tmp);
        }

        List<Article> articles = articleMapper1.getArticleList();
        for(int j=0;j<articlePos.size();j++)
        {
            for(int i=0;i<articles.size();i++)
            {
                if(Objects.equals(articles.get(i).getCategory(), articlePos.get(j).getCategory()))
                {
                    articlePos.get(j).getArticles().add(articles.get(i));
                    articles.remove(i);
                }
            }
        }

        return articlePos;
    }

    @Override
    public List<MyComment> getMyComments(String articleId) {
        try {
            int id = Integer.parseInt(articleId);
            return articleMapper1.getCommentsById(id);
        } catch (NumberFormatException e) {
            // 处理转换失败的情况
            throw new IllegalArgumentException("无效转换: " + articleId, e);
        }
    }

    @Override
    public void addMyComment(String text, String name, int findId) {
        articleMapper1.addMyComment(text,name,findId);
    }

    @Override
    public void addArticle(Article articleDTO) {
        articleMapper1.addArticle(articleDTO);
    }
}
